﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace AjaxSnippets.Configuration
{
    public class AjaxSnippetsConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("extractors", IsDefaultCollection = false)]
        public ExtractorConfigElementCollection Extractors
        {
            get
            {
                ExtractorConfigElementCollection extractors =
                (ExtractorConfigElementCollection)base["extractors"];
                return extractors;
            }
        }
    }

    public class ExtractorConfigElementCollection : ConfigurationElementCollection
    {

        protected override ConfigurationElement CreateNewElement()
        {
            return new ExtractorConfigElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ExtractorConfigElement)element).ControlType;
        }

        public ExtractorConfigElement this[int index]
        {
            get
            {
                return (ExtractorConfigElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        new public ExtractorConfigElement this[string controlType]
        {
            get
            {
                return (ExtractorConfigElement)BaseGet(controlType);
            }
        }

        public int IndexOf(ExtractorConfigElement element)
        {
            return BaseIndexOf(element);
        }

        public void Add(ExtractorConfigElement element)
        {
            BaseAdd(element);
        }

        protected override void
            BaseAdd(ConfigurationElement element)
        {
            BaseAdd(element, false);
            // Add custom code here.
        }

        public void Remove(ExtractorConfigElement element)
        {
            if (BaseIndexOf(element) >= 0)
                BaseRemove(element.ControlType);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void Clear()
        {
            BaseClear();
            // Add custom code here.
        }
    }

    public class ExtractorConfigElement : ConfigurationElement
    {
        [ConfigurationProperty("controlType", IsRequired = true, IsKey = true)]
        public string ControlType
        {
            get
            {
                return (string)this["controlType"];
            }
            set
            {
                this["controlType"] = value;
            }
        }

        [ConfigurationProperty("functionName", IsRequired = true, IsKey = false)]
        public string FunctionName
        {
            get
            {
                return (string)this["functionName"];
            }
            set
            {
                this["functionName"] = value;
            }
        }
    }
}
